package cn.wizzer.app.web.modules.controllers.platform.user.mall;

import cn.wizzer.app.user.modules.models.Goods;
import cn.wizzer.app.user.modules.models.enums.GoodsCategoryEnum;
import cn.wizzer.app.user.modules.services.GoodsService;
import cn.wizzer.app.web.commons.ulog.annotation.SLog;
import cn.wizzer.framework.base.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

@IocBean
@At("/platform/user/mall/goods")
public class GoodsController {
    private static final Log log = Logs.get();

    @Inject
    @Reference
    private GoodsService goodsService;

    @At("/")
    @Ok("beetl:/platform/user/mall/goods/index.html")
    @RequiresPermissions("user.mall.goods.list")
    public void index() {
    }


    /**
     * 商品列表(分页)
     * @param searchKeyword String 商品名
     * @param goodsCategory String 分类id
     * @param pageNumber int
     * @param pageSize int
     * @return Object
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("user.mall.goods.list")
    public Object data(@Param("searchKeyword") String searchKeyword, @Param("goodsCategory") String goodsCategory, @Param("pageNumber") int pageNumber, @Param("pageSize") int pageSize) {
        return Result.success().addData(goodsService.listPage(pageNumber, pageSize, searchKeyword, null, goodsCategory));
    }

    /**
     * 后台添加商品
     * @param goods Goods
     * @return Object
     */
    @At("/add")
    @Ok("json")
    @RequiresPermissions("user.mall.goods.add")
    @SLog(tag = "添加商品", msg = "商品名称:${args[0].name}")
    public Object add(@Param("..") Goods goods) {
        try {
            goodsService.insert(goods);
            return Result.success();
        } catch (Exception e) {
            return Result.error();
        }
    }


    /**
     * 查询商品
     * @param id String
     * @return Object
     */
    @At("/edit/?")
    @Ok("json")
    @RequiresPermissions("user.mall.goods.edit")
    public Object edit(@Param("id") String id) {
        try {
            return Result.success(goodsService.fetch(id));
        } catch (Exception e) {
            return Result.error();
        }
    }


    /**
     * 后台修改商品
     * @param goods Goods
     * @return Object
     */
    @At("/editDo")
    @Ok("json")
    @RequiresPermissions("user.mall.goods.edit")
    @SLog(tag = "修改商品", msg = "商品名称:${args[0].name}")
    public Object editDo(@Param("..") Goods goods) {
        try {
            // 乐观锁
            goodsService.updateWithVersion(goods);
            return Result.success();
        } catch (Exception e) {
            return Result.error();
        }
    }

    /**
     * 后台上架商品
     * @param id String
     * @return Object
     */
    @At("/enable/?")
    @Ok("json")
    @RequiresPermissions("user.mall.goods.edit")
    @SLog(tag = "上架商品", msg = "商品名:${args[1].getAttribute('name')}")
    public Object enable(@Param("id") String id) {
        try {
            goodsService.update(Chain.make("disabled", false), Cnd.where("id", "=", id));
            return Result.success();
        } catch (Exception e) {
            return Result.error();
        }
    }

    /**
     * 后台下架商品
     * @param id String
     * @return Object
     */
    @At("/disable/?")
    @Ok("json")
    @RequiresPermissions("user.mall.goods.edit")
    @SLog(tag = "下架商品", msg = "商品名:${args[1].getAttribute('name')}")
    public Object disable(@Param("id") String id) {
        try {
            goodsService.update(Chain.make("disabled", true), Cnd.where("id", "=", id));
            return Result.success();
        } catch (Exception e) {
            return Result.error();
        }
    }


    /**
     * 后台删除商品
     * @param id String
     * @return Object
     */
    @At({"/delete/?" ,"/delete"})
    @Ok("json")
    @RequiresPermissions("user.mall.goods.delete")
    @SLog(tag = "删除商品", msg = "商品名:${args[1].getAttribute('name')}")
    public Object delete(String id, @Param("ids") String[] ids) {
        try {
            if (null != ids && ids.length > 0) {
                goodsService.update(Chain.make("delFlag", true), Cnd.where("id", "in", ids));
            } else {
                goodsService.update(Chain.make("delFlag", true), Cnd.where("id", "=", id));
            }
            return Result.success();
        } catch (Exception e) {
            return Result.error();
        }

    }


    /**
     * 查询所有分类
     * @return Object
     */
    @At("/category")
    @Ok("json")
    public Object category() {
        try {
            return Result.success().addData(GoodsCategoryEnum.toList());
        } catch (Exception e) {
            return Result.error();
        }
    }

}

